package fr.blogger.web.outils;

import javax.faces.application.FacesMessage;
import javax.faces.application.FacesMessage.Severity;
import javax.faces.context.FacesContext;

import org.primefaces.context.RequestContext;

public abstract class UtilsJsf {

	public UtilsJsf() {
	}
	
	public static void afficherDialog(String dialog) {
		RequestContext.getCurrentInstance().execute(String.format("PF('%s').show()", dialog));
	}
	
	public static void cacherDialog(String dialog) {
		RequestContext.getCurrentInstance().execute(String.format("PF('%s').hide()", dialog));
	}

	public static void ajoutMessageInfo(String message, Object... properties) {
		ajoutMessage(FacesMessage.SEVERITY_INFO, message, properties);
	}

	public static void ajoutMessageWarning(String message, Object... properties) {
		ajoutMessage(FacesMessage.SEVERITY_WARN, message, properties);
	}

	public static void ajoutMessageError(String message, Object... properties) {
		ajoutMessage(FacesMessage.SEVERITY_ERROR, message, properties);
	}
	
	public static String getRequestParameter(String parameter) {
		return FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get(parameter);
	}

	private static void ajoutMessage(Severity severity, String message, Object... properties) {
		String messageFormat = String.format(message, properties);
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(severity, messageFormat, messageFormat));
	}

}
